<template>
	<view class="content" style="width: 100%;">

		<QSInput :name="formName" variableName="fullName" title="姓名" v-model="form.fullName" titleLayout="left" required></QSInput>
		<QSRadio :name="name" variableName="vetGender" title="性别" v-model="form.vetGender" :itemArray="radio_itemArray"
		 titleLayout="left" contentStyle="margin-right: 35%;" required></QSRadio>

		<QSInput :name="formName" variableName="phoneNum" title="手机号" leftIcon="phone" verifyType="Tel" v-model="form.phoneNum"
		 titleLayout="left" required></QSInput>
		<QSInput :name="formName" variableName="vetNumber" title="证件号" v-model="form.vetNumber" titleLayout="left" required
		 placeholder="渔业官方兽医证件号"></QSInput>
		<QSPickerCity :name="formName" variableName="city" title="区域" :value="city" placherhold="请选择" titleLayout="left"
		 required />

		<QSPickerCustom ref="pickerCustom1" :name="formName" variableName="custom" title="监督机构" placeholder="水生动物卫生监督所"
		 titleLayout="left" required v-model="form.organizationId"/>

		<QSInput :name="formName" variableName="userName" title="登陆账号" v-model="form.userName" titleLayout="left" required></QSInput>
		<QSInput :name="formName" variableName="userPassword" title="密码" v-model="form.userPassword" verifyType="password"
		 titleLayout="left" password="true" required></QSInput>
		<QSInput :name="formName" variableName="chpassword" title="确认密码" v-model="form.chpassword" titleLayout="left"
		 password="true" required></QSInput>


		<!-- 登录提交 -->
		<button class="submit" type="primary" @tap="toSubmit">提交</button>
	</view>
</template>
<script>
	import loginjs from '../../login.js'
	import QSApp from '@/components/QS-inputs-split/js/app.js';
	import province from '@/components/QS-inputs-split/template/QS-picker/city-data/province.js';
	import city from '@/components/QS-inputs-split/template/QS-picker/city-data/city.js';
	import area from '@/components/QS-inputs-split/template/QS-picker/city-data/area.js';
	import {
		mapState
	} from 'vuex'
	export default {
		computed: mapState(['baseUrl']),
		data() {
			return {
				formName: 'reg',
				form: {
					userType: 4,
					userName: null,
					fullName: null,
					userPassword: null,
					chpassword: null,
					phoneNum: null,
					address: null,
					zonePer: null,
					vetGender: null,
					organizationId: null
				},
				city: null,
				radio_itemArray: [{
						name: '男',
						value: 1,
					},
					{
						name: '女',
						value: 2
					}
				],
				newData: [],
				organizationList:[]
			}
		},
		onReady() {
			this.getInstitutionList();
		},
		methods: {
			toSubmit() {
				QSApp.getForm(this.formName).then(res => {
					if (res.verifyErr.length != 0) {
						if (!res.verifyErr[0].result) {
							if (res.verifyErr[0].variableName == 'userPassword') {
								QSApp.showToast('密码不能少于6位');
								return
							}
							if (res.verifyErr[0].variableName == 'phoneNum') {
								QSApp.showToast('手机号格式有误');
								return
							}
							QSApp.showToast('请完善表单信息');
							return
						}
					}
					if (res.data.userPassword != res.data.chpassword) {
						QSApp.showToast('两次输入的密码不一致');
						return
					}
					var value = res.data.city.value
					if (value[2] != 0) {
						var temp = area[0][value[1]][value[2]]
						this.form.zonePer = ',' + temp.value
						this.form.address = temp.value
						var temp = city[0][value[1]]
						this.form.zonePer = province[0].value + ',' + temp.value + this.form.zonePer

					} else if (value[1] != 0) {
						var temp = city[0][value[1]]
						this.form.zonePer = province[0].value + ',' + temp.value
						this.form.address = temp.value
					} else {
						this.form.zonePer = province[0].value
						this.form.address = province[0].value
					}
					
					this.form.organizationId = res.data.custom.data[0]
					this.organizationList.forEach(data=>{
						if(data.institutionName==this.form.organizationId){
							this.form.organizationId=data.id
						}
					})
					uni.showLoading({
						title: "正在提交..."
					})
					uni.request({
						url: this.baseUrl + "api/audit/audUser/vet",
						method: "POST",
						data: JSON.stringify(this.form),
						success: (res) => {
							uni.hideLoading()
							if (res.data.code == 200) {
								uni.showToast({
									title: '提交成功，管理员审核通过即可登陆',
									icon: "none",
									duration: 4000
								})
								this.toFaceLogin()
							} else {
								uni.hideLoading()
								uni.showToast({
									title: res.data.desc,
									icon: "none",
									duration: 3000
								})
							}
						},
						fail() {
							uni.hideLoading()
							uni.showToast({
								title: "请求失败",
								icon: "none",
								duration: 3000
							})
						}
					})

				}).catch(err => {
					QSApp.showToast('获取数据异常');
				});
			},
			setPickerDataFc(name, data) { 
				this.$refs[name].setData(data);
			},
			getInstitutionList() {
				uni.request({
					url: this.baseUrl + "api/institution/all",
					method: "GET",
					success: (res) => {
						this.organizationList = res.data.data
						this.newData = this.organizationList.map(items => {
							return items.institutionName
						})
						this.setPickerDataFc('pickerCustom1', [this.newData]);
					},
					fail() {
					}
				})
			},
			toFaceLogin() {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			}
		}
	}
</script>
<style>
	.submit {
		margin-top: 30px;
		margin-left: 20px;
		margin-right: 20px;
		color: white;
		background-color: #2F85FC;
		-webkit-tap-highlight-color: #2F85FC;
	}
</style>
